package com.example.wangfanwangluo.wangfanboss;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wangfanwangluo.wangfanboss.widget.SwitchButton;
import java.util.Timer;
import java.util.TimerTask;

public class MsgPwchange_login extends AppCompatActivity {
    private Typeface iconfont;
    private TextView exitbtn;
    private EditText msget;
    private EditText newpset;
    private Button getcodebtn;
    private SwitchButton switchbtn;
    private Button savebtn;
    private  static final int UPDATE_TEXT = 0;
    private Handler mHandler;
    int i = 61;
    private Timer mTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_pwchange_login);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null){
            actionbar.hide();
        }
        initViews();
        //启动定时器
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE_TEXT:
                        //在这里写想要执行的代码
                        if(i>0){
                            getcodebtn.setText("已发送"+"("+i+"s"+")");
                            getcodebtn.setBackgroundResource(R.drawable.shape_bg_btn_vcode);
                            getcodebtn.setTextColor(Color.parseColor("#999999"));}
                        else{
                            getcodebtn.setText("重新发送");
                            getcodebtn.setBackgroundResource(R.drawable.selector_bg_btn);
                            getcodebtn.setTextColor(Color.parseColor("#FFFFFF"));}
                        break;
                    default:
                        break;
                }
            }
        };


        TimerTask mTimerTask = new TimerTask() {
            @Override
            public void run() {
                /** TODO Auto-generated method stub*/
                mHandler.sendEmptyMessage(UPDATE_TEXT);
                if(i==0){
                    return;
                }
                i--;
            }
        };

        mTimer = new Timer();
        mTimer.schedule(mTimerTask, 1000, 1000);
        getcodebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //i = 0 获取验证码
                if (i == 0){
                    //网络请求，发送验证码
                    i = 61;

                }
            }
        });


    }

    //初始化ui
    public void initViews(){
        iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");
        exitbtn = (TextView)findViewById(R.id.exitbtn_msgpw);
        exitbtn.setTypeface(iconfont);
        msget = (EditText)findViewById(R.id.msg_maspw);
        newpset = (EditText)findViewById(R.id.newpw_maspw);
        getcodebtn = (Button)findViewById(R.id.getcode_msgpw);
        switchbtn = (SwitchButton)findViewById(R.id.newswicth_msgpw);
        savebtn = (Button)findViewById(R.id.savebtn_msgpw);
        exitbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //个人中心
               finish();
            }
        });
        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //核对验证码位数
                if (msget.getText().toString().length() != 6){
                    Toast.makeText(MsgPwchange_login.this,"验证码位数有误",Toast.LENGTH_SHORT).show();
                    return;
                }
                //网络请求验证
                //回到上级页面
                Intent intent = new Intent(MsgPwchange_login.this,mine.class);
                startActivity(intent);
            }
        });




    }
}
